<?php

/**
 * Created by PhpStorm.
 * User: songyao
 * Date: 2017/8/21
 * Time: 10:57
 */
class ome_mdl_gift_logs extends dbeav_model
{
    var $has_export_cnf = true;
    public $export_name = '赠品发送记录';

    function getselectdata($filter, $offset, $limit)
    {
        $list = $this->getList('*', $filter, $offset, $limit);
        $gift_rule_obj = app::get('ome')->model('gift_rule');
        $listData=array();
        foreach ($list as $k => $v) {
            $gift_rule_title = $gift_rule_obj->dump(array('id'=>$list[$k]['gift_rule_id']), 'title');
            // $list[$k]['id'] = ($list[$k]['id'] ? $list[$k]['id'] : '-');
            $listData[$k]['order_bn'] = ($list[$k]['order_bn'] ? $list[$k]['order_bn'] : '-');
            $listData[$k]['buyer_account'] = ($list[$k]['buyer_account'] ? $list[$k]['buyer_account'] : '-');
            $listData[$k]['order_source'] = ($list[$k]['order_source'] ? $list[$k]['order_source'] : '-');
            $listData[$k]['shop_id'] = ($list[$k]['shop_id'] ? $list[$k]['shop_id'] : '-');
            $listData[$k]['paid_amount'] = ($list[$k]['paid_amount'] ? $list[$k]['paid_amount'] : '-');
            $listData[$k]['goods_num'] = ($list[$k]['goods_num'] ? $list[$k]['goods_num'] : '-');
            $listData[$k]['gift_num'] = ($list[$k]['gift_num'] ? $list[$k]['gift_num'] : '-');
            $listData[$k]['send_num'] = ($list[$k]['send_num'] ? $list[$k]['send_num'] : '-');
            $listData[$k]['gift_rule_id'] = ($gift_rule_title['title'] ? $gift_rule_title['title'] : '-');
            $listData[$k]['gift_bn'] = ($list[$k]['gift_bn'] ? $list[$k]['gift_bn'] : '-');
            $listData[$k]['gift_name'] = ($list[$k]['gift_name'] ? $list[$k]['gift_name'] : '-');
            $listData[$k]['create_time'] = ($list[$k]['create_time'] ? $list[$k]['create_time'] : '-');
            $listData[$k]['update_time'] = ($list[$k]['update_time'] ? $list[$k]['update_time'] : '-');
            // $list[$k]['md5_key'] = ($list[$k]['md5_key'] ? $list[$k]['md5_key'] : '-');
            $listData[$k]['status'] = ($list[$k]['status'] ? $list[$k]['status'] : '-');
        }

        return $listData;
    }




    function getExportDataByCustom($fields, $filter, $has_detail, $curr_sheet, $start, $end)
    {

        if($curr_sheet == 1){
            $title = array();
            foreach ($this->io_title('gift') as $k => $v) {
                $title[] = mb_convert_encoding($v, 'GBK', 'UTF-8');
            }
            $data['content']['main'][0] = '"' . implode('","', $title) . '"';
        }
        $list = $this->getselectdata($filter, $start, $end);
        $omeShopMdl=app::get('ome')->model('shop');
        foreach ($list as $key => $value) {
            $list[$key]['order_bn'] .= "\t";

            $shopData=$omeShopMdl->dump(array('shop_id'=>$value['shop_id']),'name');
            $shopName='';
            if(isset($shopData['name'])){
                $shopName=$shopData['name'];
            }
            $list[$key]['shop_id'] =$shopName;
            $list[$key]['create_time'] = $value['create_time']?date('Y-m-d H:i:s', $value['create_time']):'';
            $list[$key]['update_time'] = $value['update_time']?date('Y-m-d H:i:s', $value['update_time']):'';
        }
        foreach ($list as $k => $aFilter) {
            $exptmp_data = array();
            foreach ($aFilter as $col => $val) {
                $aFilter[$col] = mb_convert_encoding($aFilter[$col], 'GBK', 'UTF-8');
                $exptmp_data[] = $aFilter[$col];
            }
            $data['content']['main'][] = implode(',', $exptmp_data);
        }
        return $data;
    }




    function io_title($filter, $ioType = 'csv')
    {
        switch ($filter) {
            case 'gift':
                $this->oSchema['csv'][$filter] = array(
                    '*:订单号' => 'order_bn',
                    '*:客户帐号' => 'buyer_account',
                    '*:订单来源' => 'order_source',
                    '*:店铺' => 'shop_id',
                    '*:付款金额' => 'paid_amount',
                    '*:购买商品总数' => 'goods_num',
                    '*:赠品数量' => 'gift_num',
                    '*:发货数量' => 'send_num',
                    '*:赠品活动名称	' => 'gift_rule_id',
                    '*:赠品编码	' => 'gift_bn',
                    '*:赠品名称	' => 'gift_name	',
                    '*:赠送时间' => 'create_time',
                    '*:发货时间' => 'update_time',
                    '*:唯一识别码' => 'md5_key',
                    '*:是否完成' => 'status',
                );
                break;
        }
        $this->ioTitle[$ioType][$filter] = array_keys($this->oSchema[$ioType][$filter]);
        return $this->ioTitle[$ioType][$filter];
    }

    // function fgetlist_csv(&$data, $filter, $offset, $exportType = 1)
    // {
    //     error_log(date("Y-m-d H-i-s") . "-" . var_export($filter, 1), 3, DATA_DIR . "/EXPORT" . date("Ymd") . ".log");
    //     if (!$data['title']) {
    //         $title = array();
    //         foreach ($this->io_title('gift') as $k => $v) {
    //             $title[] = $v;
    //         }
    //         $data['contents']['title'] = '"' . implode('","', $title) . '"';
    //     }
    //     $giftModel = app::get('ome')->model('gift_logs');
    //     foreach ($filter as $k => $v) {
    //         $giftlogs = $giftModel->dump(array('id' => $v));
    //         $gflogs_row = array();
    //         $gflogs_row['*:订单号'] = $giftlogs['order_bn'];
    //         $gflogs_row['*:客户帐号'] = $giftlogs['buyer_account'];
    //         $gflogs_row['*:订单来源'] = $giftlogs['order_source'];
    //         $gflogs_row['*:店铺'] = $giftlogs['shop_id'];
    //         $gflogs_row['*:付款金额'] = $giftlogs['paid_amount'];
    //         $gflogs_row['*:购买商品总数'] = $giftlogs['goods_num'];
    //         $gflogs_row['*:赠品数量'] = $giftlogs['gift_num'];
    //         $gflogs_row['*:发货数量'] = $giftlogs['send_num'];
    //         $gflogs_row['*:赠品活动名称'] = $giftlogs['gift_rule_id'];
    //         $gflogs_row['*:赠品编码'] = $giftlogs['gift_bn'];
    //         $gflogs_row['*:赠品名称'] = $giftlogs['gift_name'];
    //         $gflogs_row['*:赠送时间'] = $giftlogs['create_time'];
    //         $gflogs_row['*:发货时间'] = $giftlogs['update_time'];
    //         $gflogs_row['*:唯一识别码'] = $giftlogs['md5_key'];
    //         $gflogs_row['*:是否完成'] = $giftlogs['status'];

    //         $data['contents'][] = '"' . implode('","', $gflogs_row) . '"';
    //     }

    //     return false;
    // }


    public function get_schema_export()
    {
        $schema = array(
            'columns' => array(
                'order_bn' => array (
                    'type' => 'varchar(50)',
                    'required' => false,
                    'editable' => false,
                    'label' => '订单号',
                    'in_list' => true,
                    'default_in_list' => true,
                    'searchtype' => 'has',
                    'filtertype' => 'normal',
                    'filterdefault' => true,
                    'width' => 150,
                    'order' => 20
                ),
                'buyer_account' => array (
                    'type' => 'varchar(100)',
                    'required' => false,
                    'editable' => false,
                    'in_list' => true,
                    'default_in_list' => true,
                    'label' => '客户帐号',
                    'searchtype' => 'has',
                    'filtertype' => 'normal',
                    'filterdefault' => 'true',
                    'width' => 120,
                    'order' => 30
                ),
                'order_source' => array (
                    'type' => 'varchar(50)',
                    'required' => false,
                    'editable' => false,
                    'label' => '订单来源',
                    'in_list' => false,
                    'default_in_list' => false,
                    'width' => 150,
                    'order' => 40
                ),
                'shop_id' => array (
                    'type' => 'table:shop@ome',
                    'required' => false,
                    'editable' => false,
                    'label' => '店铺',
                    'in_list' => true,
                    'default_in_list' => true,
                    'width' => 150,
                    'order' => 200
                ),
                'paid_amount' => array (
                    'type' => 'money',
                    'default' => 0,
                    'required' => false,
                    'label' => '付款金额',
                    'in_list' => true,
                    'default_in_list' => true,
                    'width' => 65,
                    'order' => 50
                ),
                'goods_num' => array (
                    'type' => 'number',
                    'default' => 0,
                    'required' => false,
                    'editable' => true,
                    'label' => '购买商品总数',
                    'in_list' => false,
                    'default_in_list' => false,
                    'width' => 50,
                    'order' => 70,
                ),
                'gift_num' => array (
                    'type' => 'number',
                    'default' => 0,
                    'required' => false,
                    'editable' => true,
                    'label' => '赠品数量',
                    'in_list' => true,
                    'default_in_list' => true,
                    'width' => 65,
                    'order' => 70,
                ),
                'send_num' => array (
                    'type' => 'number',
                    'default' => 0,
                    'required' => false,
                    'editable' => true,
                    'label' => '发货数量',
                    'in_list' => false,
                    'default_in_list' => false,
                    'width' => 65,
                    'order' => 75,
                ),
                'gift_rule_id' => array (
                    'type' => 'table:gift_rule@ome',
                    'default' => 0,
                    'required' => false,
                    'editable' => true,
                    'label' => '赠品活动名称',
                    'in_list' => true,
                    'default_in_list' => true,
                    'width' => 150,
                    'order' => 80,
                ),
                'gift_bn' => array(
                    'type' => 'varchar(32)',
                    'label'=>'赠品编码',
                    'in_list' => true,
                    'default_in_list' => true,
                    'width' => 100,
                    'order' => 100,
                ),
                'gift_name' => array(
                    'type' => 'varchar(100)',
                    'label'=>'赠品名称',
                    'in_list' => true,
                    'default_in_list' => true,
                    'searchtype' => 'has',
                    'filtertype' => 'normal',
                    'filterdefault' => 'true',
                    'width' => 170,
                    'order' => 110,
                ),
                'create_time' => array(
                    'type' => 'time',
                    'label'=>'赠送时间',
                    'in_list' => true,
                    'default_in_list' => true,
                    'filtertype' => 'normal',
                    'filterdefault' => true,
                    'order' => 120,
                ),
                'update_time' => array(
                    'type' => 'time',
                    'label'=>'发货时间',
                    'in_list' => false,
                    'default_in_list' => false,
                    'order' => 130,
                ),
                'status' => array(
                    'type' => array(
                        '0'=>'未完成',
                        '1'=>'已完成',
                    ),
                    'default' => '0',
                    'label'=>'是否完成',
                    'in_list' => false,
                    'default_in_list' => false,
                    'width' => 60,
                    'order' => 150,
                ),
            ),
            'idColumn' => 'name',
            'in_list' => array(
                0 => 'order_bn',
                1 => 'buyer_account',
                2 => 'order_source',
                3 => 'shop_id',
                4 => 'paid_amount',
                5 => 'goods_num',
                6 => 'gift_num',
                7 => 'send_num',
                8 => 'gift_rule_id',
                9 => 'gift_bn',
                10 => 'gift_name',
                11 => 'create_time',
                12 => 'update_time',
                13 => 'status',
            ),
            'default_in_list' => array(
                0 => 'order_bn',
                1 => 'buyer_account',
                2 => 'order_source',
                3 => 'shop_id',
                4 => 'paid_amount',
                5 => 'goods_num',
                6 => 'gift_num',
                7 => 'send_num',
                8 => 'gift_rule_id',
                9 => 'gift_bn',
                10 => 'gift_name',
                11 => 'create_time',
                12 => 'update_time',
                13 => 'status',
            ),
        );
        return $schema;
    }



    public function getExportTitle($fields)
    {
        $title = array();
        $main_columns = $this->get_schema_export();
        foreach (explode(',', $fields) as $k => $col) {
            if (isset($main_columns['columns'][$col])) {
                $title[] = "*:" . $main_columns['columns'][$col]['label'];
            }
        }

        return mb_convert_encoding(implode(',', $title), 'GBK', 'UTF-8');
    }
}